import { Brotli } from './brotli.js';

const brotil = new Brotli();

const upLetter = 'BCHDKHK';
let upLetterArray;
const lowLetter = 'cvnvghjgjkbjhg';
let lowLetterArray;
const code = '@^&*#@^*(#&^!)#&@!(#^&@!()^#@*(!^$*(@!^$*)@!&)(!&!(*^$*(@!&*$*&^$*(@!&(),/./>[]\\/.,;';
const codeCom = '[27,83,0,0,140,212,72,77,115,59,221,212,235,22,75,136,161,84,254,194,240,249,183,37,0,96,3,78,19,192,97,208,65,100,28,135,27,48,198,85,223,24,202,177,152,112,84,98,130,233,170,229,28,49,188,196,129,18,138,58,168,63,208,18,230,96,237,62,253,188,255,215,103,95]';
let codeArray;
const numStr = '1268193682197490328590436549376894799003820043215124126819368219749032859043654937689479900382004321512412681936821974903285904365493768947990038200432151241268193682197490328590436549376894799003820043215124126819368219749032859043654937689479900382004321512412681936821974903285904365493768947990038200432151241268193682197490328590436549376894799003820043215124126819368219749032859043654937689479900382004321512412681936821974903285904365493768947990038200432151241268193682197490328590436549376894799003820043215124';
const numCom = '[27,7,2,0,140,212,72,77,115,59,55,24,155,151,50,8,128,96,49,46,113,164,246,89,239,139,189,20,214,79,17,239,164,190,253,230,90,192,47,32,22,135,202,1]';
let numArray;

function upCompressTest() {
  const upBuf = new ArrayBuffer(upLetter.length);
  const upBufView = new Uint8Array(upBuf);
  for (let i = 0; i < upLetter.length; i++) {
    upBufView[i] = upLetter.charCodeAt(i);
  }
  const upCompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    upLetterArray = brotil.compressArray(upBufView, 6);
  }
  const upCompressEndTime = Date.now();
  if (JSON.stringify(upLetterArray) != '[11,3,128,66,67,72,68,75,72,75,3]') {
    throw new Error('Brotil_CompressArray_Func_0100 error');
  }
  print(`Brotil_CompressArray_Func_0100: ${upCompressEndTime - upCompressStartTime} ms`);
}

function upDecompressTest() {
  let upDecompressed;
  const upDecompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    upDecompressed = brotil.decompressArray(upLetterArray);
  }
  const upDecompressEndTime = Date.now();
  const upRestoredStr = String.fromCharCode(...upDecompressed);
  if (upRestoredStr != upLetter) {
    throw new Error('Brotil_DecompressArray_Func_0200 error');
  }
  print(`Brotil_DecompressArray_Func_0200: ${upDecompressEndTime - upDecompressStartTime} ms`);
}

function lowCompressTest() {
  const lowBuf = new ArrayBuffer(lowLetter.length);
  const lowBufView = new Uint8Array(lowBuf);
  for (let i = 0; i < lowLetter.length; i++) {
    lowBufView[i] = lowLetter.charCodeAt(i);
  }
  const lowCompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    lowLetterArray = brotil.compressArray(lowBufView, 6);    
  }
  const lowCompressEndTime = Date.now();
  if (JSON.stringify(lowLetterArray) != '[139,6,128,99,118,110,118,103,104,106,103,106,107,98,106,104,103,3]') {
    throw new Error('Brotil_CompressArray_Func_0300 error');
  }
  print(`Brotil_CompressArray_Func_0300: ${lowCompressEndTime - lowCompressStartTime} ms`);
}

function lowDecompressTest() {
  let lowDecompressed;
  const lowDecompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    lowDecompressed = brotil.decompressArray(lowLetterArray);
  }
  const lowDecompressEndTime = Date.now();
  const lowRestoredStr = String.fromCharCode(...lowDecompressed); 
  if (lowRestoredStr != lowLetter) {
    throw new Error('Brotil_DecompressArray_Func_0400 error');
  }
  print(`Brotil_DecompressArray_Func_0400: ${lowDecompressEndTime - lowDecompressStartTime} ms`);
}

function codeCompressTest() {
  const codeBuf = new ArrayBuffer(code.length);
  const codeBufView = new Uint8Array(codeBuf);
  for (let i = 0; i < code.length; i++) {
    codeBufView[i] = code.charCodeAt(i);
  }
  const codeCompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    codeArray = brotil.compressArray(codeBufView, 6);
  }
  const codeCompressEndTime = Date.now();
  if (JSON.stringify(codeArray) != codeCom) {
    throw new Error('Brotil_CompressArray_Func_0500 error');
  }
  print(`Brotil_CompressArray_Func_0500: ${codeCompressEndTime - codeCompressStartTime} ms`);
}

function codeDecompressTest() {
  let codeDecompressed;
  const codeDecompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    codeDecompressed = brotil.decompressArray(codeArray);
  }
  const codeDecompressEndTime = Date.now();
  const codeRestoredStr = String.fromCharCode(...codeDecompressed);
  if (codeRestoredStr != code) {
    throw new Error('Brotil_DecompressArray_Func_0600 error');
  }
  print(`Brotil_DecompressArray_Func_0600: ${codeDecompressEndTime - codeDecompressStartTime} ms`);
}

function numCompressTest() {
  const numBuf = new ArrayBuffer(numStr.length);
  const numBufView = new Uint8Array(numBuf);
  for (let i = 0; i < numStr.length; i++) {
    numBufView[i] = numStr.charCodeAt(i);
  }
  const numCompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    numArray = brotil.compressArray(numBufView, 6);
  }
  const numCompressEndTime = Date.now();
  if (JSON.stringify(numArray) != numCom) {
    throw new Error('Brotil_CompressArray_Func_0700 error');
  }
  print(`Brotil_CompressArray_Func_0700: ${numCompressEndTime - numCompressStartTime} ms`);
}

function numDecompressTest() {
  let numDecompressed;
  const numDecompressStartTime = Date.now();
  for (let index = 0; index < 100; index++) {
    numDecompressed = brotil.decompressArray(numArray);
  }
  const numDecompressEndTime = Date.now();
  const numRestoredStr = String.fromCharCode(...numDecompressed);
  if (numRestoredStr != numStr) {
    throw new Error('Brotil_DecompressArray_Func_0800 error');
  }
  print(`Brotil_DecompressArray_Func_0800: ${numDecompressEndTime - numDecompressStartTime} ms`);
}

upCompressTest();
upDecompressTest();
lowCompressTest();
lowDecompressTest();
codeCompressTest();
codeDecompressTest();
numCompressTest();
numDecompressTest();
